package com.cslg.questionnaires.service;

import com.cslg.questionnaires.entity.Student;
import com.cslg.questionnaires.entity.Teacher;
import com.cslg.questionnaires.entity.User;
import com.cslg.questionnaires.mapper.StudentMapper;
import com.cslg.questionnaires.mapper.TeacherMapper;
import com.cslg.questionnaires.utils.ServiceMessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class LoginService {
    @Resource
    StudentMapper studentMapper;
    @Resource
    TeacherMapper teacherMapper;

    public Map<String,Object> studentLogin(String username, String password){
        Student student = studentMapper.getStudentByuserId(username);
        if (student == null){
            return ServiceMessageBuilder
                    .buildMessage(400,"此用户不存在！",null);
        }else{
            String truePassword = student.getStu_name();
            if (truePassword.equals(password)){
                return ServiceMessageBuilder
                        .buildMessage(200,"登录成功！",student);
            }else{
                return ServiceMessageBuilder
                        .buildMessage(401,"用户名或密码错误！",null);
            }
        }
    }

    public Map<String,Object> teacherLogin(String account, String password){
        Teacher teacher = teacherMapper.getTeacherByAccount(account);
        if (teacher == null){
            return ServiceMessageBuilder
                    .buildMessage(400,"此用户不存在！",null);
        }else{
            String truePassword = teacher.getPassword();
            if (truePassword.equals(password)){
                return ServiceMessageBuilder
                        .buildMessage(200,"登录成功！",teacher);
            }else{
                return ServiceMessageBuilder
                        .buildMessage(401,"用户名或密码错误！",null);
            }
        }
    }

}
